import { useEffect, useState } from "react";
import cloudbase from "@cloudbase/js-sdk";

function App() {
  const [cloudFunctionRes, setCloudFunctionRes] = useState("");

  const init = () => {
    const app = cloudbase.init({
      env: "", // 环境ID
    });
    app
      .auth({
        persistence: "local",
      })
      .anonymousAuthProvider()
      .signIn()
      .then((res) => {
        console.log("登录成功", res);
        app
          .callFunction({ name: "node-app" })
          .then((res) => {
            console.log(res);
            setCloudFunctionRes(`云函数调用成功,返回结果为：${res.result}`);
          })
          .catch((err) => {
            console.log(err);
            setCloudFunctionRes("云函数调用失败,请检查配置");
          });
      })
      .catch((err) => console.log("登录失败", err));
  };

  useEffect(() => {
    init();
  }, []);

  return <>{cloudFunctionRes}</>;
}

export default App;
